package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;

import play.data.validation.*;

public class Products extends Controller
{
	public static void index()
	{
		List<Product> products = Product.findAll();
	
		render(products);
	}

	public static void show(Long id)
	{
		Product product = Product.findById(id);
	
		render(product);		
    }
	
	public static void create()
	{
		render();
	}
	
	public static void save(@Valid Product product)
	{
		if(validation.hasErrors())
		{
			params.flash();
			validation.keep();
			create();
		}
	
		product.save();
		
		show(product.id);
	}
	
	public static void update(Long id)
	{
		Product product = Product.findById(id);
		
		render(product);
	}
	
	public static void doUpdate(@Valid Product product)
	{
		if(validation.hasErrors())
		{
			params.flash();
			validation.keep();
			update(product.id);
		}
	
		product.save();
		
		show(product.id);
	}
	
	public static void delete(Long id)
	{
		Product product = Product.findById(id);
		
		product.delete();
		
		index();
	}
}